<?php

namespace frontend\modules\discover\models;

/**
 * 资讯检索列表模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ArticleSearch extends \common\models\cms\CmsArticle {

    /**
     * 构建查询
     */
    public function buildQuery() {
        $query = static::findActive('t', static::STATUS_ACTIVE);
        $query->with(['stats']);
        $query->andFilterWhere(['t.channel_id' => $this->channel_id]);
        $query->addOrderBy([
            'created_at' => SORT_DESC,
        ]);
        return $query;
    }

    public function getLayout() {
        $count = count($this->thumbs);
        if ($count == 0) {
            if ($this->thumb) {
                return 1;
            }
            return 0;
        }
        if ($count == 1) {
            return 2;
        }
        return 3;
    }

    /**
     * 单例返回数据内容格式
     */
    public function fields() {
        return [
            'id',
            'layout',
            'title',
            'cover',
            'thumbs',
            'stats',
            'publishTime',
            'author',
            'weblink',
            'created_at',
        ];
    }

}
